<?php
namespace BoxAdmin\Controller;

use Think\Controller;

class MemberController extends CommonController
{
    /**
     * 会员首页
     */
    public function index()
    {
        $this->memuNav = 'index';
        $where = 'forbidden in (0,1)';
        $kw = I('kw', '', 'trim');
        if ($kw) $where .= " and (user_name like '%$kw%' or nickname like '%$kw%')";
        if ($this->store_type == 2) $where .= " and store_id = $this->store_id";
        if ($user_id = I('user_id')) {
            $where = "user_id='$user_id'";
        }
        $data = BoxModel("SystemMember")->get($where, 10);
        $this->data = $data['list'];
        $this->show_page($data['pager']);
        //$this->model("SystemMember")->where($where)->getList();
        $this->display();
    }

    /**
     * 会员等级
     */
    public function level()
    {
        $this->display();
    }

    /*****学生信息**/
    public function student_info()
    {
        $user_id = I('user_id');
        $this->gender = dict("gender");
        $this->data = Logic("BoxEdu", "User")->info($user_id);
        $this->display();
    }

    /**
     * 添加或修改会员页面
     */
    public function add()
    {
        $this->memuNav = 'index';
        $id = I('get.id', '', 'intval');
        if (!empty($id)) {
            $this->data = BoxModel("SystemMember")->load($id);
        }
        if ($this->store_type == 1) {
            $this->storeList = BoxModel('system_store')->where("store_type=2")->select();
        }
        $this->display();
    }

    /**
     * 验证用户名是否重复
     */
    public function checkName()
    {
        $username = I('username');
        $check = BoxModel("SystemMember")->getField($username, "user_name");
        if ($check) {
            echo 'false';
        } else {
            echo 'true';
        }
    }

    /**
     * 添加或修改会员
     */
    public function insert()
    {

        if (IS_POST) {
            $model = D('SystemMember');
            $data = $model->create();
            $id = I('post.id', '', 'intval');
            $password = I('password');
            if ($password) $data['password'] = encrypt($password);
            if ($id) {
                $data['user_id'] = $id;
                $model->create_member($data);
                // D('Log')->addLogs(session('user_name'),'修改会员');
            } else {
                $data['reg_time'] = time();
                $data['number'] = cnumber($this->store_id);
                $model->create_member($data);
                //  D('Log')->addLogs(session('user_name'),'添加会员');
            }
        }
        $this->redirect(U('index'));
    }

    /**
     * [delete 删除会员]
     * @return [type]
     */
    public function delete()
    {
        $id = I('get.id', '', 'intval');
        $member = D('SystemMember');
        // echo session('p');die;
        $is_del = $member->where("user_id='$id'")->setField('forbidden', 2);
        // $is_del=1;
        if ($is_del) {
            D('Log')->addLogs(session('user_name'), '删除会员');
            $this->redirect(U('index'));
        } else {
            $this->error('会员删除失败！');
        }
    }

    /**
     * [forbidden 禁用会员]
     * @return [type]
     */
    public function lock()
    {
        $id = I('get.id', '', 'intval');
        $member = D('SystemMember');
        if ($member->where("user_id='$id'")->setField('forbidden', 1)) {
            // D('Log')->addLogs(session('user_name'),'禁用会员');
            $this->redirect(U('index', array('p' => I('p'))));
        } else {
            $this->error('禁用会员失败！');
        }

    }

    /**
     * [recover 恢复会员]
     * @return [type]
     */
    public function recover()
    {
        $id = I('get.id', '', 'intval');
        $member = D('SystemMember');
        if ($member->where("user_id='$id'")->setField('forbidden', 0)) {
            // D('Log')->addLogs(session('user_name'),'恢复会员');
            $this->redirect(U('index', array('p' => I('p'))));
        } else {
            $this->error('恢复会员失败！');
        }
    }


    /**
     * 给会员发送消息
     */

    public function send_message()
    {

    }

    /**
     * 发送邮件
     */
    public function send_eamil()
    {
        if (IS_AJAX) {
            $user_name = I('user_name');
            $content = I('content');
            $EmailObj = new \Common\Org\spEmail();
            $title = "来自pointshop邮件";
            if ($EmailObj->sendmail($user_name, $title, $content)) {
                $return['user_name'] = $user_name;
                $return['status'] = 1;
            } else {
                $return['user_name'] = $user_name;
                $return['status'] = 0;
            }
            exit(json_encode($return));
        }
    }

    public function checkcity()
    {
        $areaId = I('areaId');
        $data = BoxModel('Region')->where("parent_id='$areaId'")->select();
        print_r(json_encode($data));
    }

    /**
     * 导入员工
     */
    public function import()
    {
        $this->memuNav = 'index';
        //dump($this->store_type);
        if ($this->store_type == 1) $this->storeList = BoxModel('system_store')->where('store_type=2')->select();
        $this->display();
    }

    public function importdo()
    {
        $this->memuNav = 'index';
        if (IS_POST) {
            $model = D('SystemMember');
            $store_id = I('store_id', $this->store_id);
            $file = $_FILES['excel']['tmp_name'];
            if ($file) {
                ini_set('memory_limit', '1280M');
                set_time_limit(0);
                $obj = new \Common\Org\icPHPExcel();
                $obj->Load_Excel($file);
                $data = $obj->findAll();
                $sex = array(
                    '男' => 1,
                    '女' => 2,
                );
                $sucees = 0;
                $fail = 0;
                unset($data[1]);
                if ($data) {
                    foreach ($data as $key => $value) {
                        $add = array(
                            'user_name' => $value['D'],
                            'nickname' => $value['B'],
                            'is_staff' => 2,
                            'reg_time' => time(),
                            'store_id' => $store_id,
                            'tel' => $value['C'],
                            'name' => $value['A'],
                            'phone' => $value['C'],
                            'email' => $value['D'],
                            'sex' => $sex[$value['E']] ? $sex[$value['E']] : "保密",
                            'birthday' => $value['F'],
                            'jobtype' => $value['G'],
                            'bumen' => $value['H'],
                            'number' => cnumber($store_id),
                            'is_activate' => 0,
                        );
                        $r = $model->create_member($add);
                        if ($r) {
                            $sucees++;
                            $r = "成功";
                            $class = "suc";
                        } else {
                            $fail++;
                            $r = "失败";
                            $class = "fail";
                        }
                        $msg .= "<p class='" . $class . "'>导入" . $r . " 姓名：" . $value['A'] . ",电话：" . $value['C'] . ",邮箱：" . $value['D'] . "</p>";

                    }
                    $this->msg = $msg;
                    $this->sucees = $sucees;
                    $this->fail = $fail;
                    $this->display("import_msg");
                }
            }
        }
    }

}

?>